package com.yangxd.system.controller;

import com.yangxd.response.Result;
import com.yangxd.system.service.AliOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: yangxudong
 * @Date: 2021/4/13 15:26
 * @Version 1.0
 */
@Api
@RestController
@CrossOrigin
public class AliOssController {

    @Autowired
    private AliOssService aliOssService;

    /**
     *
     * @param multipartFile
     */
    @PostMapping("/uploadImgFile")
    @ApiOperation(value = "上传用户头像")
    public Result uploadFile(MultipartFile multipartFile) {
        String res = aliOssService.upload(multipartFile);
        if(res != null) {
            return Result.ok().data("url",res);
        } else {
            return Result.error().message("上传失败！");
        }

    }

    @ApiOperation(value = "删除替换之后的头像")
    @PostMapping("/deleteImgFile")
    public Result deleteImgFile(String file) {
        //https://xinguan-parent.oss-cn-hangzhou.aliyuncs.com/2020/09/25/1575345b2cd14c13872f9b83a0aac919.png
        try {
            String[] splitFile = file.split(".com/");
            aliOssService.deleteFile(splitFile[1]);
            return Result.ok();
        }catch (Exception e){
            return Result.error();
        }
    }
}
